package com.sky.controller.user;


import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.dto.OrdersSubmitDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Api(tags = "C端-订单接口")  //swagger 接口文档
@Slf4j // log 日志对象
@RestController // 标识是一个请求处理类
@RequestMapping("user/order")
public class OrderController {

    @Autowired
    private OrdersService ordersService;

    /**
     * 用户下单
     *
     * @param dto
     * @return
     */
    @ApiOperation("用户下单")
    @PostMapping("/submit")
    public Result<OrderSubmitVO> submitOrder(@RequestBody OrdersSubmitDTO dto) {
        //1、接口入口输出日志
        log.info("用户下单(前端传递的参数):dto={}", dto);
        //2.调用service层进行逻辑处理, 并获取数据
        OrderSubmitVO orderSubmitVO = ordersService.submitOrder(dto);
        //3.相应结果
        return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     *
     * @param ordersPaymentDTO
     * @return
     */
    @PutMapping("/payment")
    @ApiOperation("订单支付")
    public Result<OrderPaymentVO> payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = ordersService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }


    /**
     * 查询订单详情
     *
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> getOrderAndDetails(@PathVariable Long id) {
        //1.入口输出日志
        log.info("查询订单详情:订单号id={}", id);
        //2.调用service层, 进行获取结果
        OrderVO orderVo = ordersService.getOrderAndDetails(id);
        //3.响应结果
        return Result.success(orderVo);
    }

    /**
     * 历史订单查询
     *
     * @param dto
     * @return
     */
    @ApiOperation("历史订单查询")
    @GetMapping("/historyOrders")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO dto) {
        //1、入口,输出日志
        log.info("历史订单查询:dto={}", dto);
        //2、调用service层获取结果
        PageResult pageResult = ordersService.userPageQuery(dto);
        //3、返回结果
        return Result.success(pageResult);
    }

    /**
     * 取消订单
     *
     * @param id
     * @return
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id) throws Exception {
        //1、入口, 输出日志, 方便调试
        log.info("取消订单:订单号id={}", id);
        //2、调用service,进行逻辑处理
        ordersService.usercancelById(id);
        //3、响应结果
        return Result.success();
    }

    /**
     * 再来一单
     *
     * @param id
     * @return
     */
    @ApiOperation("再来一单")
    @PostMapping("/repetition/{id}")
    //repetition : 重复的意思
    public Result repetition(@PathVariable Long id) {
        //1、输出日志, 关键入口
        log.info("再来一单:订单号:id={}", id);
        //2、调用service层, 做逻辑处理
        ordersService.repetition(id);
        //3、响应结果
        return Result.success();
    }

   /* *
     * 催单
     *
     * @param id
     * @return
     * */
    @ApiOperation("催单")
    @GetMapping("/reminder/{id}")
    public Result<String> reminder(@PathVariable Long id) {
        //1、输出日志
        log.info("催单: 订单号:id={}", id);
        //2、直接调用service层进行逻辑处理
        ordersService.reminder(id);
        //3、响应结果
        return Result.success("快点, 都要饿死了");
    }
}
